package com.xingkeduo.controller.api.marketing;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.controller.base.InitController;
import com.xingkeduo.model.param.ActivityUseParam;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.service.api.marketing.UserConsumeService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: UseController
 * @Description: 用户使用券，卡相关
 * @author lixin
 * @date May 2, 2019 8:10:08 PM
 *
 */
@RestController
@RequestMapping(Constant.API_URL_PREFIX + "/use")
public class UseController extends InitController {
	Logger logger = LoggerFactory.getLogger(UseController.class);
	@Autowired
	UserConsumeService userConsumeService;

	/**
	 * 
	 * @Title: placeOrder
	 * @Description: 下单，消费券，卡数据
	 * @param @param  marketingId
	 * @param @param  request
	 * @param @return 设定文件
	 * @return ResponseMessage 返回类型
	 * @throws @author lixin
	 * @date May 5, 2019 3:22:44 PM
	 */
	@PostMapping("/placeOrder")
	public ResponseMessage placeOrder(@RequestBody ActivityUseParam activityUseParam) {
		if(activityUseParam.getMarketingReceiveHistoryId()!=null) {
			MarketingTypeEnum marketingTypeEnum = MarketingTypeEnum.parseOrderStatus(activityUseParam.getMarketingType());
			if(marketingTypeEnum == MarketingTypeEnum.UNKNOWN) {
				return ResponseMessage.error("券卡类型未知",500);
			}
			activityUseParam.setMarketingType(marketingTypeEnum.getCode());
		}
		return userConsumeService.confirmOrder(activityUseParam);
	}

	/**
	 * 
	 * @Title: refundOrder
	 * @Description: 退单-下单后退单
	 * @param @param  marketingReceiveHistoryId
	 * @param @param  request
	 * @param @return 设定文件
	 * @return ResponseMessage 返回类型
	 * @throws @author lixin
	 * @date May 5, 2019 3:27:53 PM
	 */
	@PostMapping("/refundOrder")
	public ResponseMessage refundOrder(@RequestBody ActivityUseParam activityUseParam) {
		if(activityUseParam.getMarketingReceiveHistoryId()!=null) {
			MarketingTypeEnum marketingTypeEnum = MarketingTypeEnum.parseOrderStatus(activityUseParam.getMarketingType());
			if(marketingTypeEnum == MarketingTypeEnum.UNKNOWN) {
				return ResponseMessage.error("券卡类型未知",500);
			}
			activityUseParam.setMarketingType(marketingTypeEnum.getCode());
		}
		return userConsumeService.refundOrder(activityUseParam);
	}
}
